package org.example.shop_front_api.controller;

import org.example.shop_front_api.entity.Address;
import org.example.shop_front_api.service.AddressService;
import org.example.shop_front_api.utils.JsonResult;
import org.example.shop_front_api.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @PostMapping("/add")
    public JsonResult addAddress(@RequestBody Address address) {
        return addressService.addAddress(address);
    }

    @GetMapping("/list")
    public JsonResult listByUserId() {
        return addressService.listByUserId(UserContext.getUser().getId());
    }

    @GetMapping("/{id}")
    public JsonResult getById(@PathVariable Long id) {
        return addressService.getById(id);
    }

    @PutMapping("/update/{id}")
    public JsonResult updateAddress(@PathVariable Long id, @RequestBody Address address) {
        address.setId(id);
        return addressService.updateAddress(address);
    }

    @DeleteMapping("/delete/{id}")
    public JsonResult deleteAddress(@PathVariable Long id) {
        return addressService.deleteAddress(id);
    }
}